﻿using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace DesktopToyWpf.Convertors;

public struct WeatherImageType()
{
    public string Name { get; set; } = "晴";
    public bool IsDay { get; set; } = true; // 是否为白天
}

[ValueConversion(typeof(WeatherImageType), typeof(BitmapImage))]
public class WeatherToBitmapImageConvertor : IValueConverter
{
    public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture)
    {
        return value is not WeatherImageType type ? null : GetWeatherImage(type.Name, type.IsDay);
    }

    private static BitmapImage GetWeatherImage(string weatherName, bool isDay)
    {
        // 参考：https://worldweather.wmo.int/zh/wxicons.html
        return weatherName switch
        {
            // 太阳/月亮
            "晴" => LoadImage(isDay ? "2" : "13"),
            "少云" => LoadImage(isDay ? "2" : "13"),
            "平静" => LoadImage(isDay ? "2" : "13"),
            "有风" => LoadImage(isDay ? "2" : "13"),
            "微风" => LoadImage(isDay ? "2" : "13"),
            "和风" => LoadImage(isDay ? "2" : "13"),
            "清风" => LoadImage(isDay ? "2" : "13"),

            // 多云
            "晴间多云" => LoadImage(isDay ? "3" : "4"),
            "阴" => LoadImage("1"),
            "多云" => LoadImage("1"),

            // 大风
            "强风/劲风" => LoadImage("9"),
            "疾风" => LoadImage("9"),
            "大风" => LoadImage("9"),
            "烈风" => LoadImage("9"),
            "风暴" => LoadImage("9"),
            "狂爆风" => LoadImage("9"),
            "飓风" => LoadImage("9"),
            "热带风暴" => LoadImage("9"),
            "龙卷风" => LoadImage("9"),

            // 霾
            "霾" => LoadImage("12"),
            "中度霾" => LoadImage("12"),
            "重度霾" => LoadImage("12"),
            "严重霾" => LoadImage("12"),

            // 雨
            "阵雨" => LoadImage(isDay ? "5" : "10"),
            "雷阵雨" => LoadImage("6"),
            "雷阵雨并伴有冰雹" => LoadImage("11"),
            "小雨" => LoadImage("7"),
            "中雨" => LoadImage("7"),
            "大雨" => LoadImage("7"),
            "暴雨" => LoadImage("7"),
            "大暴雨" => LoadImage("7"),
            "特大暴雨" => LoadImage("7"),
            "强阵雨" => LoadImage("7"),
            "强雷阵雨" => LoadImage("7"),
            "极端降雨" => LoadImage("7"),
            "毛毛雨/细雨" => LoadImage("7"),
            "雨" => LoadImage("7"),
            "小雨-中雨" => LoadImage("7"),
            "中雨-大雨" => LoadImage("7"),
            "大雨-暴雨" => LoadImage("7"),
            "暴雨-大暴雨" => LoadImage("7"),
            "大暴雨-特大暴雨" => LoadImage("7"),
            "雨雪天气" => LoadImage("11"),
            "雨夹雪" => LoadImage("11"),
            "阵雨夹雪" => LoadImage("11"),
            "冻雨" => LoadImage("11"),

            // 雪
            "雪" => LoadImage("8"),
            "阵雪" => LoadImage("8"),
            "小雪" => LoadImage("8"),
            "中雪" => LoadImage("8"),
            "大雪" => LoadImage("8"),
            "暴雪" => LoadImage("8"),
            "小雪-中雪" => LoadImage("8"),
            "中雪-大雪" => LoadImage("8"),
            "大雪-暴雪" => LoadImage("8"),

            // 尘/霾
            "浮尘" => LoadImage("12"),
            "扬沙" => LoadImage("12"),
            "沙尘暴" => LoadImage("12"),
            "强沙尘暴" => LoadImage("12"),
            "雾" => LoadImage("12"),
            "浓雾" => LoadImage("12"),
            "强浓雾" => LoadImage("12"),
            "轻雾" => LoadImage("12"),
            "大雾" => LoadImage("12"),
            "特强浓雾" => LoadImage("12"),
            "热" => LoadImage("2"),
            "冷" => LoadImage("8"),

            // 其他情况
            "未知" => LoadImage(isDay ? "2" : "13"),
            _ => LoadImage(isDay ? "2" : "13")
        };
    }


    private static readonly Dictionary<string, BitmapImage> ImageCache = new();
    
    private static BitmapImage LoadImage(string imageName)
    {
        if (!ImageCache.ContainsKey(imageName))
        {
            ImageCache.Add(imageName, new BitmapImage(new Uri($"pack://application:,,,/Images/Weather/{imageName}.png")));
        }

        return ImageCache[imageName];
    }


    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotSupportedException();
}